import UniversalRequestQueue from './UniversalRequestQueue';
import * as utils from '@/common/comFun/utils.js';
import APP from '@/common/http/request.js';

const createQueueMethod = (methodName) => function(...args) {
	let current = this;
	while (current) {
		if (current.$requestQueue) {
			return current.$requestQueue[methodName](...args);
		}
		current = current.$parent;
	}
	console.error('Request queue not found in component tree');
	return () => {};
};

export default {
	install(Vue) {
		Vue.mixin({
			beforeCreate() {
				// 增强的页面组件判断逻辑
				const isPageComponent = this.$options && this.$options.mpType === 'page';

				if (isPageComponent) {
					// 确保实例化队列
					this.$requestQueue = new UniversalRequestQueue(APP, utils);

					// 开发调试辅助
					// if (process.env.NODE_ENV === 'development') {
					const pages = getCurrentPages()
					const currentPage = pages[pages.length - 1]
					const route = currentPage.route // 获取路由路径

					// console.log('[RequestQueue] 页面队列已初始化:', route);
					this._requestQueue = this.$requestQueue; // 暴露给控制台
					// }
				}
			},
			destroyed() {
				// 添加安全判断
				if (this.$requestQueue && typeof this.$requestQueue.destroy === 'function') {
					const pages = getCurrentPages()
					const currentPage = pages[pages.length - 1]
					const route = currentPage.route // 获取路由路径
					// console.log('[RequestQueue] 销毁页面队列:', route);
					this.$requestQueue.destroy();
					this.$requestQueue = null;
				}
			}
		});

		const methodMap = {
			$post: 'request',
			$requestByUrl: 'requestByUrl',
			$requestByUrlSub: 'requestByUrlSub',
			$request: 'originalRequest',
			$promiseRequest: 'promiseRequest',
			$promiseRequestByUrl: 'promiseRequestByUrl',
			$upload: 'upload'
		};

		Object.entries(methodMap).forEach(([vueMethod, queueMethod]) => {
			Vue.prototype[vueMethod] = createQueueMethod(queueMethod);
		});

		Vue.prototype.$cancelRequest = function(requestId) {
			let current = this;
			while (current) {
				if (current.$requestQueue) {
					current.$requestQueue.cancel(requestId);
					return;
				}
				current = current.$parent;
			}
			console.warn('No active request queue found');
		};

		Vue.prototype.$clearQueue = function(queueId = 'default') {
			let current = this;
			while (current) {
				if (current.$requestQueue) {
					current.$requestQueue.clear(queueId);
					return;
				}
				current = current.$parent;
			}
		};

		Vue.prototype.$onQueueEvent = function(event, callback) {
			let current = this;
			while (current) {
				if (current.$requestQueue) {
					current.$requestQueue.on(event, callback);
					return () => current.$requestQueue.off(event, callback);
				}
				current = current.$parent;
			}
			return () => {};
		};

		Vue.prototype.$offQueueEvent = function(event, callback) {
			let current = this;
			while (current) {
				if (current.$requestQueue) {
					current.$requestQueue.off(event, callback);
					return;
				}
				current = current.$parent;
			}
		};

		// if (process.env.NODE_ENV === 'development') {
		Vue.prototype.$requestQueue = function() {
			let current = this;
			while (current) {
				if (current.$requestQueue) return current.$requestQueue;
				current = current.$parent;
			}
			return null;
		};
		// }
	}
};